package com.hengda.smart.speak.d.stc;

import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.hengda.platform.easyhttp.subscriber.HttpSubscriber;
import com.hengda.smart.speak.d.App;
import com.hengda.smart.speak.d.R;
import com.hengda.smart.speak.d.bean.Interpreter;
import com.hengda.smart.speak.d.config.AppConfig;
import com.hengda.smart.speak.d.util.SharePreferenceUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

import android_serialport_api.SerialPort;

/**
 *
 * @Description STC数传服务接收底层上传的数据
 * @author wzq
 * @date 2015-6-11 下午3:22:19
 * @update (date)
 * @version V1.0
 */

public class ControlService extends Service implements StcManager {

	// 1==1 stc用
	private App mApplication;
	protected SerialPort mSerialPort;
	protected OutputStream mOutputStream;
	private InputStream mInputStream;
	private ReadThread mReadThread;
	private boolean runFlag = true;// 线程标志
	private StcLauncher stcLauncher; // 发送STC指令UTIL
	// 回传参数字符品串
	private byte[] tem1 = new byte[4];
	private byte[] tem2 = new byte[4];

	// 2 ==handler MsgWhat
	private static final int IR_AUTONUM_ = 1;
	private static final int RFID_AUTONUM_ = 2;
	private static final int PLAYTIME_ = 3;
	private static final int START_SCREENINFO = 4;
	private static final int OPEN_AUTO_SUCESS = 5; // 打开音频成功
	private static final int OPEN_AUTO_FAILED = 6; // 打开音频失败
	private static final int CHANNEL_EROR_1 = 8; // 收到红外号码
	private static final int CHANNEL_EROR_2 = 9; // 红外中途冲突 **/
	private static final int CHANNEL_EROR_3 = 10; // 红外中途冲突 **/
	private static final int UPLOAD_POSITON = 11; // 上传当前占用逻辑群组编号

	// 3==服务中向外播出的广播
	private Intent openSucessIntent, openFailedIntent;
	private Intent channelErrorIntent1, channelErrorIntent2,
			channelErrorIntent3, channelIRIntent1; // 数传返回 1.红外自动适配 2.首次冲突
	// 3.中途冲突
	private Intent consoleAction;// 测试回传参数显示
	private Intent intent_IR = null;
	private Intent intent_RFID = null;
	private int errorChannel;
	private int rfid_num = 0;
	private int ir_num = 0;
	public static final String ACTION_REICEIVE_RFID = "ACTION_REICEIVE_RFID_PLAY";
	private Intent rfidReceiveIntent;

	// 4== 服务中的其他工具
	// 存储配置工具
	private SharePreferenceUtil spUtil;
	private int i = 0;
	// 当前上传成功的逻辑群主编号
	private int currentSucAutoNo = 0;
	private int UPLOAD_DELEY = 4000;
	//讲解员信息
	private Interpreter interpreter;
//	private WebApiCd apiCd;
	private HttpSubscriber mHttpObserverObj;



	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
//		KJLoger.debug("********STC服务开启**********");
		mApplication = App.getInstance();
//		apiCd = new WebApiCd(this);
		stcLauncher = new StcLauncher(mApplication, getApplicationContext());
		spUtil = new SharePreferenceUtil(getApplicationContext(),
				AppConfig.SHARE_NAME);
		try {
			mSerialPort = mApplication.getSerialPort();
			mInputStream = mSerialPort.getInputStream();
			mReadThread = new ReadThread();
			this.start();
		} catch (SecurityException e) {
			DisplayError(R.string.error_security);
		} catch (IOException e) {
			DisplayError(R.string.error_unknown);
		} catch (InvalidParameterException e) {
			DisplayError(R.string.error_configuration);
		}
		initIntent();
	}

	private void initIntent() {
		openSucessIntent = new Intent(LancherConstant.BROADCAST_AUDIO_SUCCESS);
		openFailedIntent = new Intent(LancherConstant.BROADCAST_AUDIO_FAILURE);
		intent_IR = new Intent("HENGDA.IR_AUTONUM_.BROADCAST");
		intent_RFID = new Intent("HENGDA.RFID_AUTONUM_.BROADCAST");
		consoleAction = new Intent(LancherConstant.BROADCAST_CONSOLE_TEST);
		channelErrorIntent1 = new Intent(LancherConstant.BROADCAST_ERROR_FIRST);
		channelIRIntent1 = new Intent(LancherConstant.BROADCAST_INFRARED_CHANNEL);
		channelErrorIntent2 = new Intent(LancherConstant.BROADCAST_ERROR_HALFWAY);
		channelErrorIntent3 = new Intent(LancherConstant.BROADCAST_CONSOLE_TEST);
		rfidReceiveIntent = new Intent(ACTION_REICEIVE_RFID);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	/**
	 * 启动线程
	 */
	private void start() {
		mReadThread.start();
		// mHandler.sendEmptyMessageDelayed(80, 10000);
	}

	/**
	 * 停止线程
	 */
	private void stop() {
		runFlag = false;
	}

	/**
	 * @Description 获取底层传入数据的线程
	 * @author wzq
	 * @date 2015-6-11 下午3:23:25
	 * @update (date)
	 * @version V1.0
	 */

	private class ReadThread extends Thread {
		@Override
		public void run() {
			super.run();
			while (true) // !isInterrupted()
			{
				int size;
				byte[] buffer = new byte[16];
				try {
					if (mInputStream == null) {
						return;
					}
					size = 0;
					size = mInputStream.read(buffer);

					if (size > 0) {
						if (size == 4) {
							if (buffer[0] == (byte) 0xAA) {
								tem1 = subBytes(buffer, 0, 4);
							} else {
								tem2 = subBytes(buffer, 0, 4);
								if (tem1 != null) {
									onDataReceived(byteMerger(tem1, tem2), 8);
								}
							}
						} else {
							onDataReceived(buffer, size);
						}
					}
					try {
						sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} catch (IOException e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}

	private void DisplayError(int resourceId) {
		AlertDialog.Builder b = new AlertDialog.Builder(this);
		b.setTitle("Error");
		b.setMessage(resourceId);
		b.setPositiveButton("OK", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				stopSelf();
			}
		});
		b.show();
	}

	@Override
	public void onDataReceived(byte[] buffer, int size) {
		System.err
				.println(CommonHexUtil.bytesToHexString(buffer) + "=====" + size);
		consoleAction.putExtra("Test", CommonHexUtil.bytesToHexString(buffer));
		sendBroadcast(consoleAction);
		for (int m = 0; m < 8; m++) {
			if (buffer[m] == (byte) 0xAA && buffer[m + 1] == (byte) 0x55) {
				// rfid
				if ((buffer[m + 2] == (byte) 0x05)
						&& buffer[m + 6] == (buffer[m + 4] ^ buffer[m + 5])) {
					rfid_num = (buffer[m + 4] & 0x0FF) * 256
							+ (buffer[m + 5] & 0x0FF);

					/************ 防盗报警 ********************/
					if (rfid_num == 2003) {
					}
					if (rfid_num == 2001 || rfid_num == 2002) {
					}
					/*************************************/
					mHandler.sendEmptyMessage(RFID_AUTONUM_);
					break;
				} else if ((buffer[m + 2] == (byte) 0x01)
						&& buffer[m + 3] == (byte) 0x02) { // 音频模块返回
					if ((buffer[m + 4] == (byte) 0x01)
							&& buffer[m + 6] == (buffer[m + 4] ^ buffer[m + 5])) { // 800M
						// 模块打开成功
						mHandler.sendEmptyMessage(OPEN_AUTO_SUCESS);

					} else if ((buffer[m + 4] == (byte) 0x02)
							&& buffer[m + 6] == (buffer[m + 4] ^ buffer[m + 5])) { // 800M模块打开失败
						mHandler.sendEmptyMessage(OPEN_AUTO_FAILED);
					}

				} else if ((buffer[m + 2] == (byte) 0x02)
						&& buffer[m + 3] == (byte) 0x02) { // 数传模块返回
//					KJLoger.debug(CommonHexUtil.bytesToHexString(buffer));
					if ((buffer[m + 4] == (byte) 0x01)
							&& buffer[m + 7] == (buffer[m + 4] ^ buffer[m + 5] ^ buffer[m + 6])) { // 红外自动适配
						int irNo = (buffer[m + 5] & 0xFF) * 256
								+ (buffer[m + 6] & 0xFF); // 群组号
						try {
							if (spUtil.getCurrentTaskId().equals("")) {

							} else {
								// UpLoadPosition(irNo);
							}
						} catch (Exception e) {
						}

					} else if ((buffer[m + 4] == (byte) 0x05) // 20151028
							// 增加占用通道编号回传
							&& buffer[m + 7] == (buffer[m + 4] ^ buffer[m + 5] ^ buffer[m + 6])) {
						int hostNo = (buffer[m + 5] & 0xFF) * 256
								+ (buffer[m + 6] & 0xFF); // 群组号
//						KJLoger.debug("当前+=" + hostNo + "本地="
//								+ spUtil.getHostNo());
						if (hostNo != 9999 && hostNo != spUtil.getHostNo()) {
							spUtil.setHostNo(hostNo);
							spUtil.setLocalAuto(hostNo + "");
						}

					} else if ((buffer[m + 4] == (byte) 0x02)
							&& buffer[m + 7] == (buffer[m + 4] ^ buffer[m + 5] ^ buffer[m + 6])) { // 首次冲突

					} else if ((buffer[m + 4] == (byte) 0x03)
							&& buffer[m + 7] == (buffer[m + 4] ^ buffer[m + 5] ^ buffer[m + 6])) { // 中途冲突
					}
				}
			}
		}
	}


	//// TODO: 2017/6/6
//	/**
//	 *  上传位置信息
//	 * @param currentNo
//	 * @param taskId
//	 */
//
//	private void uploadMyPosition(final int currentNo, String taskId) {
//		interpreter = DbCdHelper.getUser();
//		Map<String, Object> maps = new HashMap<>();
//		maps.put("limit", "0");
//		maps.put("num", "0");
//		maps.put("language", "1");
//		HttpManager.getInstance().basePost("index.php?a=travel_list", maps, mHttpObserverObj, TravelFoodParentBean.class, HttpManager.OBJ);
////		if (interpreter != null && spUtil.getLoginState()) {
////			apiCd.uploadPosition(interpreter.getJobnum(), taskId, currentNo + "").subscribe(new Observer<BaseData>() {
////				@Override
////				public void onCompleted() {
////
////				}
////
////				@Override
////				public void onError(Throwable e) {
////
////				}
////
////				@Override
////				public void onNext(BaseData baseData) {
////					if (baseData.getCode().equals("000")) {
////						currentSucAutoNo = currentNo;
////						ViewInject.toast(baseData.getMsg());
////					} else {
////						ViewInject.toast(baseData.getMsg());
////					}
////
////				}
////			});
////		}
//	}



	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case IR_AUTONUM_:
					if (ir_num > 0) {
						intent_IR.putExtra("IR_", ir_num);
						sendBroadcast(intent_IR);
					}
					break;
				case RFID_AUTONUM_:
					if (rfid_num > 0) {
						intent_RFID.putExtra("RFID_", rfid_num);
						sendBroadcast(intent_RFID);
					}
					break;
				case OPEN_AUTO_SUCESS:
					sendBroadcast(openSucessIntent);
//					ViewInject.toast("打开音频成功");
					break;
				case OPEN_AUTO_FAILED:
					sendBroadcast(openFailedIntent);
//					ViewInject.toast("打开音频失败");
					break;
				case CHANNEL_EROR_1:
					channelErrorIntent1.putExtra(
							LancherConstant.FIELD_ERROR_CHANNEL, errorChannel);
					sendBroadcast(channelErrorIntent1);
					break;

				case UPLOAD_POSITON:
					if (currentSucAutoNo != spUtil.getHostNo()) {
//						uploadMyPosition(spUtil.getHostNo(),spUtil.getCurrentTaskId());
					}
					sendEmptyMessageDelayed(UPLOAD_POSITON, UPLOAD_DELEY);
					break;

				case 80:

					try {
						if (spUtil.getCurrentTaskId().equals("")) {
						} else {
//							uploadMyPosition(i,spUtil.getCurrentTaskId());
						}
					} catch (Exception e) {
					}
					i++;
					sendEmptyMessageDelayed(80, 10000);
					break;
			}
		}

	};

	/**
	 *
	 * @Description:合并两个字节数组、解决异常
	 * @param byte_1
	 * @param byte_2
	 * @return
	 * @return byte[]
	 * @throws
	 * @autour wzq
	 * @date 2015-6-23 下午6:50:50
	 * @update (date)
	 */

	public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
		byte[] byte_3 = new byte[byte_1.length + byte_2.length];
		System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
		System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
		return byte_3;
	}

	/**
	 * 从一个byte[]数组中截取一部分0
	 *
	 * @param src
	 * @param begin
	 * @param count
	 * @return
	 */
	public static byte[] subBytes(byte[] src, int begin, int count) {
		byte[] bs = new byte[count];
		for (int i = begin; i < begin + count; i++)
			bs[i - begin] = src[i];
		return bs;
	}



}
